/*******************************************************************************
 * copyright (c) 2009 - 2010 Simon Allen
 *
 * This file is part of Convex Media Converter.
 *
 * Convex Media Converter is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Convex Media Converter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Convex Media Converter; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 ******************************************************************************/


#include "../constants.h"
#include "fileproperties.h"
#include "ui_fileproperties.h"

#include <MediaInfoDLL/MediaInfoDLL.h>

using namespace std;
using namespace MediaInfoDLL;


FileProperties::FileProperties(const QFileInfo& currentFile, QWidget *parent) : QDialog(parent), ui(new Ui::FileProperties)
{
	QString durationString;
	MediaInfo info;

	ui->setupUi(this);

	setWindowIcon(QIcon(CONVEX_LOGO));
	setWindowTitle(currentFile.filePath());

	info.Open(currentFile.filePath().toStdString());

	ui->label_inputTypeDesc->setText(info.Get(Stream_General, 0, "Format").c_str());
	ui->label_inputSizeDesc->setText(QString::number(currentFile.size() / 1000000.0, 'f', 2) + " MB");

	long duration = QString(info.Get(Stream_General, 0, "Duration").c_str()).toLong();
	int secs = duration / 1000;

	int hours = secs / 3600;
	secs -= hours * 3600;
	int mins = secs / 60;
	secs -= mins * 60;

	durationString = QString::number(hours) + "h " + QString::number(mins) + "m "
			 + QString::number(secs) + "s";

	ui->label_inputDurationDesc->setText(durationString);

	// We have audio.
	if (!info.Get(Stream_Audio, 0, "Format").empty())
	{
		ui->label_inputAudioCodecDesc->setText(info.Get(Stream_Audio, 0, "Format").c_str());

		if (!info.Get(Stream_Audio, 0, "BitRate").empty())
			ui->label_inputAudioBitrateDesc->setText(info.Get(Stream_Audio, 0, "BitRate").c_str());

		else
			ui->label_inputAudioBitrateDesc->setText("N/A");

		if (!info.Get(Stream_Audio, 0, "BitRate_Mode").empty())
			ui->label_inputAudioModeDesc->setText(info.Get(Stream_Audio, 0, "BitRate_Mode").c_str());

		else
			ui->label_inputAudioModeDesc->setText("N/A");

		if (!info.Get(Stream_Audio, 0, "SamplingRate").empty())
			ui->label_inputSampleRateDesc->setText(info.Get(Stream_Audio, 0, "SamplingRate").c_str());

		else
			ui->label_inputSampleRateDesc->setText("N/A");

		if (!info.Get(Stream_Audio, 0, "Channels").empty())
			ui->label_inputAudioChannelsDesc->setText(info.Get(Stream_Audio, 0, "Channels").c_str());

		else
			ui->label_inputAudioChannelsDesc->setText("N/A");
	}

	else
	{
		ui->label_inputAudioCodecDesc->setText("N/A");
		ui->label_inputAudioBitrateDesc->setText("N/A");
		ui->label_inputSampleRateDesc->setText("N/A");
		ui->label_inputAudioChannelsDesc->setText("N/A");
	}

	// We have video.
	if (!info.Get(Stream_Video, 0, "Format").empty())
	{
		if (!info.Get(Stream_Video, 0, "Format").empty())
			ui->label_inputVideoCodecDesc->setText(info.Get(Stream_Video, 0, "Format").c_str());

		else
			ui->label_inputVideoCodecDesc->setText("N/A");


		if (!info.Get(Stream_Video, 0, "BitRate").empty())
			ui->label_inputVideoBitrateDesc->setText(info.Get(Stream_Video, 0, "BitRate").c_str());

		else
			ui->label_inputVideoBitrateDesc->setText("N/A");

		if ((!info.Get(Stream_Video, 0, "Width").empty()) &&
		   (!info.Get(Stream_Video, 0, "Height").empty()))
			ui->label_inputResolutionDesc->setText((info.Get(Stream_Video, 0, "Width")
							       + "x" + info.Get(Stream_Video, 0, "Height")).c_str());

		else
			ui->label_inputResolutionDesc->setText("N/A");

		if (!info.Get(Stream_Video, 0, "AspectRatio").empty())
			ui->label_inputAspectRatioDesc->setText(info.Get(Stream_Video, 0, "AspectRatio").c_str());

		else
			ui->label_inputAspectRatioDesc->setText("N/A");

		if (!info.Get(Stream_Video, 0, "FrameRate").empty())
			ui->label_inputFrameRateDesc->setText(QString(info.Get(Stream_Video, 0, "FrameRate").c_str()) + " fps");

		else
			ui->label_inputFrameRateDesc->setText("N/A");
	}

	else
	{
		ui->label_inputVideoCodecDesc->setText("N/A");
		ui->label_inputVideoBitrateDesc->setText("N/A");
		ui->label_inputResolutionDesc->setText("N/A");
		ui->label_inputAspectRatioDesc->setText("N/A");
		ui->label_inputFrameRateDesc->setText("N/A");
	}
}

FileProperties::~FileProperties()
{
	delete ui;

	ui = NULL;
}
